﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Estimate.Common.DataImporter
{
    
    /// <summary>
    /// 实况数据质量计数器
    /// 保存错误的数目，以及错误信息
    /// </summary>
    public class ImportDataQuality
    {
        /// <summary>
        /// 错误信息列表
        /// </summary>
        private List<string> _errorMessage;

        /// <summary>
        /// 错误信息数目
        /// </summary>
        public int ErrorCount
        {
            get
            {
                return _errorMessage.Count;
            }
        }

        public string ErrorString
        {
            get
            {
                return string.Join(",", _errorMessage.ToArray());
            }
        }

        /// <summary>
        /// 需要确认的站点。
        /// 如果站点已经确认存在，则值为true
        /// 否则值为false
        /// </summary>
        private Dictionary<string, bool> _providedStations;

        /// <summary>
        /// 缺失数目
        /// </summary>
        public int LackCount
        {
            get
            {
                int result = 0;
                foreach (bool value in _providedStations.Values)
                {
                    if (!value)
                        result++;
                }
                return result;
            }
        }

        /// <summary>
        /// 缺少的站点字符串
        /// </summary>
        public string LackString
        {
            get
            {
                List<string> result = new List<string>(2);
                foreach (KeyValuePair<string,bool> pair in _providedStations)
                {
                    if (!pair.Value)
                        result.Add(pair.Key);
                }
                if (result.Count == 0)
                    return "";
                return string.Join(",", result.ToArray());
            }
        }

        private string[] _stations;

        /// <summary>
        /// 提供必须的站点的数目。保留信息
        /// </summary>
        /// <param name="stations">站点数组</param>
        public ImportDataQuality(string[] stations)
        {
            _errorMessage = new List<string>();
            _providedStations = new Dictionary<string, bool>();
            _stations = stations;

            Clear();
        }

        /// <summary>
        /// 恢复原始状态
        /// </summary>
        public void Clear()
        {
            _errorMessage.Clear();
            foreach (string s in _stations)
            {
                //所有的站点都未提供
                _providedStations[s] = false;
            }
        }

        /// <summary>
        /// 添加一个错误信息
        /// </summary>
        /// <param name="msg">错误信息</param>
        public void AddError(string msg)
        {
            _errorMessage.Add(msg);
        }

        /// <summary>
        /// 提供了站点
        /// </summary>
        /// <param name="station">置站点信息</param>
        public void ProvideStation(string station)
        {
            if (_providedStations.ContainsKey(station))
            {
                _providedStations[station] = true;
            }
        }

        /// <summary>
        /// 丢失了站点
        /// </summary>
        /// <param name="station">置站点信息</param>
        public void MissStation(string station)
        {
            if (_providedStations.ContainsKey(station))
            {
                _providedStations[station] = false;
            }
        }

        /// <summary>
        /// 检查站点是否在统计范围内
        /// </summary>
        /// <param name="station">站点站号</param>
        /// <returns>是否存在</returns>
        public bool ExistStation(string station)
        {
            return _providedStations.ContainsKey(station);
        }

        


    }
}
